{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np \n",
    "import time\n",
    "from IPython.display import clear_output\n",
    "\n",
    "from six.moves.urllib.request import urlopen\n",
    "from contextlib import closing\n",
    "import json\n",
    "import k3d\n",
    "\n",
    "plot = k3d.plot()\n",
    "plot.display()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from k3d.helpers import download\n",
    "from pyunpack import Archive\n",
    "\n",
    "filename = download('http://www.semantic3d.net/data/point-clouds/testing1/stgallencathedral_station1_intensity_rgb.7z')\n",
    "Archive(filename).extractall('./')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import csv\n",
    "\n",
    "data = None\n",
    "\n",
    "with open(filename.replace('.7z', '.txt'), mode='r') as csv_file:\n",
    "    csv_reader = csv.reader(csv_file, delimiter=' ')    \n",
    "    data = np.array(list(csv_reader), dtype=np.float32)\n",
    "\n",
    "# compute color in hex format\n",
    "data[:, 4] = np.sum(data[:, 4:7].astype(np.uint32) * np.array([1, 256, 256 ** 2]), axis=1)    \n",
    "data = data[:, 0:5]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "data.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot += k3d.points(data[::2, 0:3], data[::2, 4].astype(np.uint32), point_size=0.05, shader=\"flat\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = [5.251483149143791,\n",
    " -7.92683507646606,\n",
    " 3.144285796928443,\n",
    " -2.470283607444292,\n",
    " 3.6558150584160503,\n",
    " 2.3721091212696286,\n",
    " 0,\n",
    " 0,\n",
    " 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera_auto_fit = False\n",
    "plot -= plot.objects[0]\n",
    "plot += k3d.points(data[::50, 0:3], data[::50, 4].astype(np.uint32), point_size=0.25, shader=\"flat\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "sigma=10.0\n",
    "beta=8./3\n",
    "rho=28.0\n",
    "def lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho):\n",
    "    \"\"\"Compute the time-derivative of a Lorenz system.\"\"\"\n",
    "    x, y, z = X.T\n",
    "    return np.vstack([sigma * (y - x), x * (rho - z) - y, x * y - beta * z]).T"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "plot.camera = [82.36534387751811,\n",
    " -119.8210969123126,\n",
    " 43.968748841328704,\n",
    " -0.7272701043451865,\n",
    " 4.817824060482123,\n",
    " 35.65948744314234,\n",
    " 0,\n",
    " 0,\n",
    " 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for p in plot.objects:\n",
    "    X = p.positions\n",
    "    for i in range(150):\n",
    "        X = X + lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho)*0.002\n",
    "        if i%15==0 and i>0:\n",
    "            p.positions = X[::1,:]\n",
    "        #time.sleep(0.1)\n",
    "        clear_output(wait=True)\n",
    "        print(\"iteration:\",i)\n",
    "    p.positions = X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(15):\n",
    "    for p in plot.objects:\n",
    "        X = p.positions\n",
    "        for j in range(15):\n",
    "            X = X + lorenz_deriv(X, sigma=sigma, beta=beta, rho=rho)*0.001\n",
    "        p.positions = X[:,:]\n",
    "        clear_output(wait=True)\n",
    "        print(\"iteration:\",i)\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "p.point_size = .65"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
